package com.xhwl.logistics.strategy.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.common.dto.cent.vehicle.feign.CreateBiVehicleCardAccessRecordHourFeignDTO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.entity.VehicleArea;
import com.xhwl.logistics.po.VehicleCardAccessRecordStatisticsPO;
import com.xhwl.logistics.service.impl.VehicleAreaServiceImpl;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;

import java.util.Date;
import java.util.List;

/**
 * 大华Icc的项目同步出行记录到bi中台
 */
@Slf4j
//@Service
public class VehicleParkingLotDaHuaIccStatisticsHandlerImpl extends VehicleParkingLotDefaultStatisticsHandlerImpl {

    @Autowired
    private VehicleAreaServiceImpl vehicleAreaService;

    /**
     * 根据区域名称统计出行记录
     * @param vehicleIntegratedConfigBO
     */
    @Async("threadPoolTaskExecutorForSchedule")
    @Override
    public void statisticsCardAccessRecordRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        Date processDate = new Date();
        Date ingestionDate = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(processDate, VehicleConstant.vehicleTimeZone), 1);
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

        log.info("statistics card access start!,project id: {}", projectId);

        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = queryParkingLot(projectId);
        String parkingLotName = vehicleParkingLotSecondBO.getParkingLotName();
        String parkId = vehicleParkingLotSecondBO.getParkId();

        //统计所有区域的进出记录
        List<VehicleCardAccessRecordStatisticsPO> cardAccessRecordForIn = vehicleAccessRecordMapper.accessRecordForCountSecond(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForEnter(projectId, null, ingestionDate));
        List<VehicleCardAccessRecordStatisticsPO> cardAccessRecordForOut = vehicleAccessRecordMapper.accessRecordForCountSecond(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForGoOut(projectId, null, ingestionDate));
        CreateBiVehicleCardAccessRecordHourFeignDTO createBiVehicleCardAccessRecordHourFeignDTO = vehicleStatisticsConverter.toCreateBiVehicleCardAccessRecordHourFeignDTO(projectId, parkingLotName, processDate, ingestionDate, cardAccessRecordForIn, cardAccessRecordForOut);

        biVehicleService.addDwsCardAccessRecordForHour(createBiVehicleCardAccessRecordHourFeignDTO);
        //统计各个区域的进出记录
        List<VehicleArea> vehicleAreaList = vehicleAreaService.list(conditionForAreaFromParkingLotId(vehicleParkingLotSecondBO.getId()));
        //由于一个车场的区域很少，所以使用for循环
        for (VehicleArea vehicleArea : vehicleAreaList) {
            List<VehicleCardAccessRecordStatisticsPO> cardAccessRecordForIn1 = vehicleAccessRecordMapper.accessRecordForCountSecond(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForEnter(projectId, vehicleArea.getRemark(), ingestionDate));
            List<VehicleCardAccessRecordStatisticsPO> cardAccessRecordForOut1 = vehicleAccessRecordMapper.accessRecordForCountSecond(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForGoOut(projectId, vehicleArea.getRemark(), ingestionDate));
            CreateBiVehicleCardAccessRecordHourFeignDTO createBiVehicleCardAccessRecordHourFeignDTO1 = vehicleStatisticsConverter.toCreateBiVehicleCardAccessRecordHourFeignDTO(projectId, vehicleArea.getRemark(), processDate, ingestionDate, cardAccessRecordForIn1, cardAccessRecordForOut1);
            biVehicleService.addDwsCardAccessRecordForHour(createBiVehicleCardAccessRecordHourFeignDTO1);
        }

        log.info("statistics card access end!,project id: {}", projectId);
    }

    @Override
    public void statisticsCarportRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {

    }

    @Async("threadPoolTaskExecutorForSchedule")
    @Override
    public void statisticsLocationAccessRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        super.statisticsLocationAccessRecordToBi(vehicleIntegratedConfigBO);
    }

    @Override
    public void statisticsCardPayRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {

    }

    /**
     * 通过车场id查询该车场下的所有区域信息
     */
    private Wrapper<VehicleArea> conditionForAreaFromParkingLotId(Integer parkingLotId){
        return new LambdaQueryWrapper<VehicleArea>().eq(
                VehicleArea::getParkingLotId,parkingLotId
        );
    }
}
